<?php
namespace app\api\utils;

use think\Request;

class Utils
{

    private static $code = [
        '0000' => '成功',
        '0001' => '失败',
        '0101' => '请求参数为空',
        '0102' => '系统故障',
        '0103' => '参数传入错误',
    ];

    private static $msg    = '';
    private static $status = '0000';
    private static $result = [];

    /**
     * 验证请求方法
     * @param  array  $allowMethod 允许通过的请求方法
     * @return [type]              [description]
     */
    public static function validateRequestMethod(array $allowMethod)
    {
        if (!in_array(strtolower(Request::instance()->method()), $allowMethod)) {
            self::$status = '0001';
            self::$msg = '非法请求';
        }
        return new static;
    }

    /**
     * 验证接收参数
     * @param  bool   $hasParam  是否有参数 
     * @param  array  $necessary 必要的需验证的参数
     * @return [type]            [description]
     */
    public static function validateParams (bool $hasParam, array $necessary)
    {
        $params = Request::instance()->param();
        if ($hasParam) {
            if ($necessary) {
                $paramKey = array_keys($params);
                foreach ($necessary as $value) {
                    if (!is_array($value)) {
                        if (!in_array($value, $paramKey)) {
                            self::$status = '0103';
                            self::$msg = '缺少参数' . $value;
                            break;
                        }
                    } else {
                        foreach ($value as $subField=>$subValue) {
                            $subParamKey = is_string($params[$subField]) ?
                                 json_decode($params[$subField], true) : $params[$subField];
                            if ($params[$subField] && is_null($subParamKey)) {
                                self::$status = '0001';
                                self::$msg = 'JSON格式不正确';
                                break;
                            }
                            $subParamKey = array_keys($subParamKey);
                            foreach ($subValue as $param) {
                                if (!in_array($param, $subParamKey)) {
                                    self::$status = '0103';
                                    self::$msg = '缺少参数' . $param;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        return new static;
    }


    /**
     * 设置提示信息
     * @param string $msg [description]
     */
    public static function setMsg (string $msg)
    {
        self::$msg = $msg;
        return new static;
    }

    /**
     * 接口应答格式化
     * @param  string $status 状态码
     * @param  array  $result 返回数据
     * @param  string $msg    提示信息
     * @return [type]        
     */
    public static function response (array $result = [], array $extra = [])
    {
        if (self::$status !== '0000'){
            $result = [];
            $extra  = [];
        }
        $msg = self::$msg ? self::$msg : self::$code[self::$status];
        $res =  [

            'status'  => self::$status,
            'message' => $msg,
            'result'  => $result,
        ];
        $res = $extra ? array_merge($res, $extra) : $res;
        return json($res);
    }
}